/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Account;
import client.entity.CVDetail;
import client.entity.Category;
import client.entity.Cv;
import client.entity.Education;
import client.entity.EmploymentHistory;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Worker;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class LoadCVByID {

    /**
     * Creates a new instance of LoadCVByID
     */
    public LoadCVByID() {
    }
    private CVDetail cvdetail;
    private int id;

    public void setCvdetail(CVDetail cvdetail) {
        this.cvdetail = cvdetail;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getParam(String param) {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public CVDetail getCvdetail() {
        String pr = getParam("idCVDetail");
        if (pr == null) {
            id = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("idCVDetail");
        } else {
            id = Integer.parseInt(pr);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("idCVDetail", id);
        }
        cvdetail= new CVDetail();
        cvdetail.setCv(getCVById(id));
        cvdetail.setCategory(getListCategory(id));
        cvdetail.setLeveljob(getLevelJobByIDCV(id));
        cvdetail.setListCompany(getListCompanyByCvId(id));
        cvdetail.setListEducation(getListEducationByCvId(id));
        cvdetail.setLocation(getListLocation(id));
        cvdetail.setWorker(getWorkerByIDCv(id));
        return cvdetail;
    }

    public List<EmploymentHistory> getListCompanyByCvId(int idCV) {
        List<EmploymentHistory> list = new ArrayList<EmploymentHistory>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("from EmploymentHistory e where e.cv.id=:idcv").setParameter("idcv", idCV).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Education> getListEducationByCvId(int idCV) {
        List<Education> list = new ArrayList<Education>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("from Education ed where ed.cv.id=:idcv").setParameter("idcv", idCV).list();       
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Category> getListCategory(int idCV) {
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        List tempList = ss.createSQLQuery("SELECT c.ID,c.Category_Name  FROM CV_Category cc INNER JOIN Category c ON cc.ID_Category=c.ID WHERE cc.ID_CV=" + idCV)
                .addScalar("ID", Hibernate.INTEGER).addScalar("Category_Name", Hibernate.STRING).list();
        for (Object objectRc : tempList) {
            Category rl = new Category();
            Object[] lstOrc = (Object[]) objectRc;
            rl.setId(Integer.parseInt(lstOrc[0].toString()));
            rl.setCategoryName(lstOrc[1].toString());
            list.add(rl);
        }        
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Location> getListLocation(int idCV) {
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        List tempList = ss.createSQLQuery("SELECT lc.ID,lc.Name FROM CV_Location cvl INNER JOIN Location lc ON cvl.ID_Location=lc.ID WHERE cvl.ID_CV=" + idCV)
                .addScalar("ID", Hibernate.INTEGER).addScalar("Name", Hibernate.STRING).list();
        for (Object objectRc : tempList) {
            Location rl = new Location();
            Object[] lstOrc = (Object[]) objectRc;
            rl.setId(Integer.parseInt(lstOrc[0].toString()));
            rl.setName(lstOrc[1].toString());
            list.add(rl);
        }
        ss.getTransaction().begin();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public Cv getCVById(int idCV) {
        Cv cv = new Cv();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        cv = (Cv) ss.createQuery("from Cv c where c.id=:id").setParameter("id", idCV).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return cv;
    }

    public Worker getWorkerByIDCv(int idCV) {
        Worker wk = new Worker();      
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();        
        Object idWK=ss.createSQLQuery("SELECT ID_Worker FROM CV c WHERE c.ID="+idCV).addScalar("ID_Worker",Hibernate.INTEGER).uniqueResult();
        wk = (Worker) ss.createQuery("from Worker w where w.id=:id").setParameter("id", (Integer)idWK).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return wk;
    }
    public LevelJob getLevelJobByIDCV(int idCV){
        LevelJob lj = new LevelJob();     
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        Object idLJ=ss.createSQLQuery("SELECT c.ID_Level_Job FROM CV c WHERE c.ID="+idCV).addScalar("ID_Level_Job",Hibernate.INTEGER).uniqueResult();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id=:id").setParameter("id", idLJ).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    } 
    
    public String backAction() {
//        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
//                .getExternalContext().getSession(false);
         Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
        if (ac == null) {
            NavigationHandler nh = FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
            nh.handleNavigation(FacesContext.getCurrentInstance(), null, "login.xhtml");
        } else {
           
            return "workerProfile.xhtml?accId=" + ac.getId() + "&faces-redirect=true";
        }
        return null;

    }
}
